// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function () {
    // 初始化测验数据
    initQuiz();
    // 绑定按钮点击事件
    setupButtonEvents();
});

// 初始化测验数据
function initQuiz() {
    // 模拟测验题目数据
    currentQuiz.questions = [
        {
            question: "这是第一道测验题目，答案选A吗？",
            options: ["A. 是的", "B. 不是", "C. 不确定"],
            answer: 0
        },
        {
            question: "这是第二道测验题目，答案选B吗？",
            options: ["A. 不是", "B. 是的", "C. 可能吧"],
            answer: 1
        },
        {
            question: "这是第三道测验题目，答案选C吗？",
            options: ["A. 不是", "B. 也许", "C. 是的"],
            answer: 2
        }
    ];
    // 初始化答案数组
    currentQuiz.answers = new Array(currentQuiz.questions.length).fill(null);
    // 显示第一道题目
    showQuestion(0);
}

// 显示指定索引的题目
function showQuestion(index) {
    const questionElement = document.getElementById('question');
    const optionsElement = document.getElementById('options');

    // 检查索引是否越界
    if (index < 0 || index >= currentQuiz.questions.length) {
        return;
    }

    const question = currentQuiz.questions[index];
    // 显示题目
    questionElement.textContent = question.question;
    // 清空选项
    optionsElement.innerHTML = '';

    // 遍历选项并添加到页面
    question.options.forEach((option, optionIndex) => {
        const label = document.createElement('label');
        const input = document.createElement('input');
        input.type = 'radio';
        input.name = 'answer';
        input.value = optionIndex;
        // 如果之前已经选择过，恢复选中状态
        if (currentQuiz.answers[index] === optionIndex) {
            input.checked = true;
        }
        label.appendChild(input);
        label.appendChild(document.createTextNode(option));
        optionsElement.appendChild(label);
    });

    // 更新按钮状态
    updateButtonState(index);
}

// 更新按钮状态
function updateButtonState(index) {
    const prevButton = document.getElementById('prev-btn');
    const nextButton = document.getElementById('next-btn');

    // 第一题时，上一题按钮禁用
    prevButton.disabled = index === 0;
    // 最后一题时，下一题按钮文本变为提交
    if (index === currentQuiz.questions.length - 1) {
        nextButton.textContent = '提交';
    } else {
        nextButton.textContent = '下一题';
    }
}

// 绑定按钮点击事件
function setupButtonEvents() {
    const prevButton = document.getElementById('prev-btn');
    const nextButton = document.getElementById('next-btn');

    // 上一题按钮点击事件
    prevButton.addEventListener('click', function () {
        if (currentQuiz.currentQuestionIndex > 0) {
            saveAnswer();
            currentQuiz.currentQuestionIndex--;
            showQuestion(currentQuiz.currentQuestionIndex);
        }
    });

    // 下一题按钮点击事件
    nextButton.addEventListener('click', function () {
        saveAnswer();
        if (currentQuiz.currentQuestionIndex < currentQuiz.questions.length - 1) {
            currentQuiz.currentQuestionIndex++;
            showQuestion(currentQuiz.currentQuestionIndex);
        } else {
            // 提交答案
            submitQuiz();
        }
    });
}

// 保存当前题目的答案
function saveAnswer() {
    const selectedOption = document.querySelector('input[name="answer"]:checked');
    if (selectedOption) {
        currentQuiz.answers[currentQuiz.currentQuestionIndex] = parseInt(selectedOption.value);
    }
}

// 提交测验答案
function submitQuiz() {
    let correctCount = 0;
    currentQuiz.questions.forEach((question, index) => {
        if (currentQuiz.answers[index] === question.answer) {
            correctCount++;
        }
    });

    // 更新用户测验记录
    userQuizRecord.correctCount = correctCount;
    userQuizRecord.totalCount = currentQuiz.questions.length;

    // 显示结果
    const resultElement = document.getElementById('result');
    resultElement.textContent = `你答对了 ${correctCount} 道题，共 ${currentQuiz.questions.length} 道题。`;
    resultElement.style.display = 'block';
}
